package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiuer
 * @description 购物车控制层
 * @create 2019/7/12 15:07
 */
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * http://api.leyou.com/api/cart
     * 商品加入购物车
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * http://api.leyou.com/api/cart/list
     * 请求方式get 参数没有 返回值responseEntity<cart>\
     *
     * @description 查询用户的购物车信息
     */
    @GetMapping("list")
    public ResponseEntity<List<Cart>> queryUserCart() {
        return ResponseEntity.ok(cartService.queryUserCart());
    }

    /**
     * 修改购物车商品数量
     * @param id
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("id") String id,
                                           @RequestParam("num") Integer num) {
        cartService.updateCart(id,num);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *
     * @param id  skuId
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteCart(@PathVariable("id")String id) {
        cartService.deleteCart(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 登陆后购物车合并
     */
    @PostMapping("list")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> carts) {
        cartService.addCarts(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
}
